package team.zero.lemon.common.persistence;

import com.fasterxml.jackson.annotation.JsonFormat;
import team.zero.lemon.common.date.UTC;
import team.zero.lemon.common.security.session.SessionContext;
import team.zero.lemon.common.security.session.model.Session;
import team.zero.lemon.common.utils.IdGen;

import java.util.Date;

/**
 * Created by bolin on 2015/5/18.
 */
public class DataEntity extends BaseEntity {
    private static final long serialVersionUID = 1L;

    public DataEntity(Integer id) {
        super(id);
    }

    public DataEntity() {
        super();
    }

    private Integer sort;
    private String remark;
    private Integer isDel;
    private Integer creator;
    private Date createTime;
    private Integer updator;
    private Date updateTime;

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getIsDel() {
        return isDel;
    }

    public void setIsDel(Integer isDel) {
        this.isDel = isDel;
    }

    public Integer getCreator() {
        return creator;
    }

    public void setCreator(Integer creator) {
        this.creator = creator;
    }
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getCreateTime() {
        return createTime;
    }
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getUpdator() {
        return updator;
    }

    public void setUpdator(Integer updator) {
        this.updator = updator;
    }
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getUpdateTime() {
        return updateTime;
    }
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * 插入之前执行方法，需要手动调用
     */
    @Override
    public void preInsert() {
        // 不限制ID为UUID，调用setisInsert()使用自定义ID
        this.isDel = 0;
        if (this.creator == null) {
            Session session = SessionContext.getSession();
            if (session == null) {
                this.creator = 0;
            } else {
                this.creator = session.getId();
            }
        }
        this.updator = this.creator;
        this.updateTime = UTC.date();
        this.createTime = this.updateTime;
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    @Override
    public void preUpdate() {
        if (this.updator == null) {
            Session session = SessionContext.getSession();
            if (session == null) {
                this.updator = 0;
            } else {
                this.updator = session.getId();
            }
        }
        this.updateTime = UTC.date();
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    @Override
    public void preDelete() {
        this.isDel = 1;
        if (this.updator == null) {
            Session session = SessionContext.getSession();
            if (session == null) {
                this.updator = 0;
            } else {
                this.updator = session.getId();
            }
        }
        this.updateTime = UTC.date();
    }
}